import pymysql
from dbutils.pooled_db import PooledDB
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='root',
                             database='assistant',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)
def querystu():
    cursor = connection.cursor()
    sql='select * from stu'
    cursor.execute(sql)
    result=cursor.fetchall()
    return result
def addstu(name,sex,age,xi,major):
    cursor = connection.cursor()
    sql = "insert into stu(name,sex,age,xi,major) values(%s,%s,%s,%s,%s)"
    cursor.execute(sql, (name,sex,age,xi,major))
    connection.commit()
def updatestu(id,name,sex,age,xi,major):
    cursor = connection.cursor()
    sql = "update stu set name=%s,sex=%s,age=%s,xi=%s,major=%s where id=%s"
    cursor.execute(sql,(name,sex,age,xi,major,id))
    connection.commit()
def deletestu(id):
    cursor = connection.cursor()
    sql = "delete from stu where id=%s"
    cursor.execute(sql,int(id))
    connection.commit()

def queryxi():
    cursor = connection.cursor()
    sql='select * from xi'
    cursor.execute(sql)
    result=cursor.fetchall()
    return result
def addxi(xi):
    cursor = connection.cursor()
    sql = "insert into xi(xi) values(%s)"
    cursor.execute(sql, (xi))
    connection.commit()
def updatexi(id,xi):
    cursor = connection.cursor()
    sql = "update xi set xi=%s where id=%s"
    cursor.execute(sql,(xi,id))
    connection.commit()
def deletexi(id):
    cursor = connection.cursor()
    sql = "delete from xi where id=%s"
    cursor.execute(sql,int(id))
    connection.commit()


def querymajor():
    cursor = connection.cursor()
    sql='select * from major'
    cursor.execute(sql)
    result=cursor.fetchall()
    return result
def addmajor(xi,major):
    cursor = connection.cursor()
    sql = "insert into major(xi,major) values(%s,%s)"
    cursor.execute(sql, (xi,major))
    connection.commit()
def updatemajor(id,xi,major):
    cursor = connection.cursor()
    sql = "update major set xi=%s,major=%s where id=%s"
    cursor.execute(sql,(xi,major,id))
    connection.commit()
def deletemajor(id):
    cursor = connection.cursor()
    sql = "delete from major where id=%s"
    cursor.execute(sql,int(id))
    connection.commit()




class MySQLConnectionPool:

    def __init__(self, ):
        self.pool = PooledDB(
            creator=pymysql,  # 使用链接数据库的模块
            mincached=10,  # 初始化时，链接池中至少创建的链接，0表示不创建
            maxconnections=200,  # 连接池允许的最大连接数，0和None表示不限制连接数
            blocking=True,  # 连接池中如果没有可用连接后，是否阻塞等待。True，等待；False，不等待然后报错
            host = 'localhost',
            user = 'root',
            password = 'root',
            database = 'assistant',
            charset = 'utf8',
            cursorclass = pymysql.cursors.DictCursor
        )

    def open(self):
        self.conn = self.pool.connection()
        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)  # 表示读取的数据为字典类型
        return self.conn, self.cursor

    def close(self, cursor, conn):
        cursor.close()
        conn.close()

    def select_one(self, sql, args):
        """查询单条数据"""
        conn, cursor = self.open()
        cursor.execute(sql, args)
        result = cursor.fetchone()
        self.close(conn, cursor)
        return result

    def select_many(self, sql, args):
        """查询多条数据"""
        conn, cursor = self.open()
        cursor.execute(sql, args)
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result

    def select_all(self, sql):
        conn, cursor = self.open()
        cursor.execute(sql)
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result

    def insert_one(self, sql, args):
        """插入单条数据"""
        res = self.execute(sql, args, isNeed=True)
        return res

    def insert_all(self, sql, datas):
        """插入多条批量插入"""
        conn, cursor = self.open()
        try:
            cursor.executemany(sql, datas)
            conn.commit()
            return {'result': True, 'id': int(cursor.lastrowid)}
        except Exception as err:
            conn.rollback()
            return {'result': False, 'err': err}

    def update_one(self, sql, args):
        """更新数据"""
        self.execute(sql, args, isNeed=True)

    def delete_one(self, sql, *args):
        """删除数据"""
        self.execute(sql, args, isNeed=True)

    def execute(self, sql, args, isNeed=False):
        """
        执行
        :param isNeed 是否需要回滚
        """
        conn, cursor = self.open()
        if isNeed:
            try:
                cursor.execute(sql, args)
                conn.commit()
            except:
                conn.rollback()
            finally:
                return cursor.rowcount
        else:
            cursor.execute(sql, args)
            conn.commit()
        self.close(conn, cursor)




mysql = MySQLConnectionPool()


